/**
 * 
 */
package com.p2p.base.exception;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import com.p2p.base.util.Const;

/**
 * @author ruiqi
 *
 */
public class BizException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6660397787108948632L;
	
	private String code;
	
	static Properties msgprop = new Properties(); 
	static Map<String,String> msgmap = new HashMap<String,String>();
	static boolean hasInit = false;
	/**
	 * 
	 */
	public BizException(String code) {
		this.code = code;
	}
	public BizException(String code,String message) {
		super(message);
		this.code = code;
	}

	/**
	 * @param cause
	 */
	public BizException(Throwable cause) {
		super(cause);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param message
	 * @param cause
	 */
	public BizException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param message
	 * @param cause
	 * @param enableSuppression
	 * @param writableStackTrace
	 */
	public BizException(String message, Throwable cause,
			boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO Auto-generated constructor stub
	}



	public String getCode() {
		return code;
	}



	public void setCode(String code) {
		this.code = code;
	}
	public static String getGlobalMessage(String key) {
		if(!hasInit){
			init();
		}
	//	String configPath ="D:/Users/ruiqi/git/p2p/yiqihao/config/businessException";
		//String msg = ResourceBundle.getBundle(configPath2+Const.ERR_MSG, LocaleContextHolder.getLocale()).getString(key);
		String msg = msgprop.getProperty(key);
		return msg;
	}
	private static void init(){
		String configPath2 = System.getProperty("p2p.config_path");
		if(configPath2.startsWith("file:")){
			configPath2 = configPath2.replaceFirst("file:", "");
		}
		InputStream in;
		try {
			in = new BufferedInputStream(new FileInputStream(configPath2+Const.ERR_MSG+".properties"));
			msgprop.load(in);
			for(Entry<Object,Object> entry:msgprop.entrySet()){
				msgmap.put(entry.getKey().toString(), entry.getValue().toString());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		hasInit = true;
	}

	public static Map<String,String> getClobalAllMessage(){
		if(!hasInit){
			init();
		}
		
		return msgmap;
	}
	
}
